//
//  ChatPromptView.m
//  FlyingEagle
//
//  Created by Lj on 2017/5/5.
//  Copyright © 2017年 lj. All rights reserved.
//

#import "ChatPromptView.h"
#define buttonClickTag 57656

@interface ChatPromptView()

@property (nonatomic, strong) UIView *subBackView;
@property (nonatomic, strong) UIView *backView;

@property (nonatomic, strong) NSString *titleStr;
@property (nonatomic, strong) NSString *messageStr;
@property (nonatomic, strong) NSMutableArray *buttonArray;

@end

@implementation ChatPromptView


- (id)initWithFrame:(CGRect)frame title:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle {
    if (self = [self initWithFrame:frame]) {
        _buttonArray = [NSMutableArray array];
        _titleStr = title;
        _messageStr = message;
        if (![BHSettingUtil dataAndStringIsNull:cancelButtonTitle]) {
            [_buttonArray addObject:cancelButtonTitle];
        }
        if (![BHSettingUtil dataAndStringIsNull:otherButtonTitle]) {
            [_buttonArray addObject:otherButtonTitle];
        }
        [self creatView];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    }
    return self;
}

- (void)creatView {
    
    _subBackView = [[UIView alloc]initWithFrame:self.frame];
    _subBackView.backgroundColor = [UIColor colorWithWhite:0 alpha:.1];
    [BHSettingUtil addTapGestureInView:_subBackView addTarget:self action:@selector(backViewClick) withTag:32323];
    [self addSubview:_subBackView];
    
    
    UIView *backView = [[UIView alloc]init];
    backView.center = CGPointMake(App_Frame_Width/2, App_Frame_Height/2);
    backView.bounds = CGRectMake(0, 0, App_Frame_Width - 30, 166);
    backView.backgroundColor = [UIColor colorWithHexString:@"#CBCBCB"];
    backView.layer.cornerRadius = 5;
    backView.clipsToBounds = YES;
    [self addSubview:backView];
    _backView = backView;
    
    UIImageView *cacleImage = [[UIImageView alloc]init];
    cacleImage.image = [UIImage imageNamed:@"icon-quxiao-30"];
    [backView addSubview:cacleImage];
    [cacleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView).offset(15);
        make.right.equalTo(backView.mas_right).offset(- 15);
        make.height.width.offset(20);
    }];
    UIButton *cacleButton = [[UIButton alloc]init];
    [cacleButton addTarget:self action:@selector(cacleClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cacleButton];
    [cacleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView).offset(0);
        make.right.equalTo(backView.mas_right).offset(0);
        make.height.width.offset(50);
    }];
    
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = _titleStr;
    titleLabel.font = BHFont_Size_18;
    titleLabel.textColor = [UIColor whiteColor];
    [backView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView).offset(20);
        make.centerX.equalTo(backView.mas_centerX);
    }];
    
    //描述
    UILabel *messageLabel = [[UILabel alloc]init];
    NSArray *array = [_messageStr componentsSeparatedByString:@"'"];
//    messageLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    messageLabel.textColor = [UIColor whiteColor];
    if (array.count == 0) {
        messageLabel.text = _messageStr;
    }else {
        _messageStr = [_messageStr stringByReplacingOccurrencesOfString:@"'" withString:@""];
        NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:_messageStr];
        for (int i = 0; i < array.count/2; i ++) {
            NSRange range = [[hintString string]rangeOfString:array[i * 2 + 1]];
            [hintString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#F45833"] range:range];
        }
        messageLabel.attributedText = hintString;
    }
    messageLabel.font = BHFont_Size_15;
    messageLabel.numberOfLines = 0;
    messageLabel.textAlignment = NSTextAlignmentLeft;
    [backView addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(15);
        make.centerX.equalTo(backView.mas_centerX);
        make.left.equalTo(backView).offset(15);
    }];
//    [self createButtonView];
}

- (void)createButtonView {
    if (_buttonArray.count == 1) {
        UIButton *button = [[UIButton alloc]init];
        [button setTitle:_buttonArray[0] forState:UIControlStateNormal];
        button.backgroundColor = Main_Color;
        button.layer.cornerRadius = 5;
        button.clipsToBounds = YES;
        button.tag = buttonClickTag;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_backView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_backView).offset(25);
            make.right.equalTo(_backView.mas_right).offset(- 25);
            make.bottom.equalTo(_backView.mas_bottom).offset(- 15);
            make.height.offset(45);
        }];
    }else if (_buttonArray.count == 2) {
        for (int i = 0; i < 2; i ++) {
            CGFloat bWidth = _backView.frame.size.width;
            UIButton *button = [[UIButton alloc]init];
            [button setTitle:_buttonArray[i] forState:UIControlStateNormal];
            button.backgroundColor = Main_Color;
            button.layer.cornerRadius = 5;
            button.clipsToBounds = YES;
            button.tag = buttonClickTag + i;
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [_backView addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_backView).offset(13 + i * (bWidth - 36)/2 + 10 * i);
                make.width.offset((bWidth - 36)/2);
                make.bottom.equalTo(_backView.mas_bottom).offset(- 15);
                make.height.offset(45);
            }];
        }
    }
}

#pragma mark - show
- (UIView *)topView {
    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    return window.subviews[0];
}

- (void)show {
    [UIView animateWithDuration:0.6 animations:^{
        _backView.alpha = 1;
        _subBackView.alpha = 0.5;
    } completion:^(BOOL finished) {
        
    }];
    [[self topView]addSubview:self];
    [self showAnimation];
}

#pragma mark - Animation
- (void)showAnimation {
    CAKeyframeAnimation *alertViewShowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    alertViewShowAnimation.duration = 0.6;
    alertViewShowAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)],[NSValue valueWithCATransform3D:CATransform3DIdentity]];
    alertViewShowAnimation.keyTimes = @[@0.2f,@0.5f,@0.75f,@1.0f];
    alertViewShowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [_backView.layer addAnimation:alertViewShowAnimation forKey:nil];
}

#pragma mark - 取消视图
- (void)cancelView {
    [UIView animateWithDuration:0.5 animations:^{
        _backView.alpha = 0.0;
        _subBackView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - 按钮
//背景点击事件
- (void)backViewClick {
    //  [self cancelView];
}

//取消按钮
- (void)cacleClick {
    [self cancelView];
}

//底部按钮
- (void)buttonClick:(UIButton *)sender {
    [self cancelView];
}

@end

